package Offer_06;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/3/30 9:53
 */
public class Solution {
    public static void main(String[] args) {

    }

    static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
        }
    }


    public int[] reversePrint(ListNode head) {
        Deque<ListNode> stack = new LinkedList<>();
        ListNode tem = head;
        while (tem != null) {
            stack.push(tem);
            tem=tem.next;
        }
        int[] arr = new int[stack.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = stack.pop().val;
        }
        return arr;
    }

    public int[] reversePrint1(ListNode head) {
        Deque<Integer> stack = new LinkedList<>();
        while(head!= null){
            stack.push(head.val);
            head=head.next;
        }
        int[] arr = new int[stack.size()];
        for(int i = 0; i< arr.length;i++){
            arr[i] = stack.pop();
        }
        return arr;
    }
    ArrayList<Integer> arr = new ArrayList<>();
    public int[] reversePrint2(ListNode head) {
        reCall(head);

        int[] res = new int[arr.size()];
        for (int i = 0; i < res.length; i++) {
            res[i] = arr.get(i);
        }
        return res;
    }
    void reCall (ListNode h){
        if (h==null){
            return;
        }
        reCall(h.next);
        arr.add(h.val);
    }
}
